package com.choosefine.it.company.service.Impl;

import java.util.Map;

import com.choosefine.it.basedata.model.UserInfo;
import com.choosefine.it.basedata.service.CountService;
import com.choosefine.it.manager.model.ManagerFindprojectInfo;
import com.choosefine.it.manager.model.dto.ManagerFindprojectInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.company.dao.CompanyFindManagerMapper;
import com.choosefine.it.company.model.CompanyFindManagerInfo;
import com.choosefine.it.company.service.CompanyFindManagerService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * @author chenjian
 * @date 2016-12-3 上午10:10:34
 */
@Service("companyFindManagerService")
public class CompanyFindManagerServiceImpl implements CompanyFindManagerService {
	@Autowired
	private CompanyFindManagerMapper companyFindManagerMapper;
	@Autowired
	MongoTemplate mongoTemplate;
	@Autowired
	CountService countService;
	@SuppressWarnings("rawtypes")
	/*建筑公司-找项目承包人列表和搜索yuxin3.11*/
	@Override
	public Page<CompanyFindManagerInfo> companyFindManagerList(Map<String, Object> params, PageDTO pageDto) {
		PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize());
		Page<CompanyFindManagerInfo> page = companyFindManagerMapper.companyFindManagerList(params);
		return page;
	}
	/*建筑公司-项目承包人的详情页yuxin3.11*/
	@Override
	public ManagerFindprojectInfoDTO findCompanyFindManagerInfoById(Map<String, Object> params){
		ManagerFindprojectInfoDTO managerFindprojectInfoDTO=null;
		ManagerFindprojectInfo managerFindprojectInfo=mongoTemplate.findOne(new Query(Criteria.where("_id").is(params.get("publishId"))), ManagerFindprojectInfo.class);
		if(managerFindprojectInfo!=null) {
			int isInsert=countService.isInsert(params);
			managerFindprojectInfo.setIsInsert(isInsert);
			UserInfo userInfo = mongoTemplate.findOne(new Query(Criteria.where("userCode").is(managerFindprojectInfo.getUserCode())), UserInfo.class);
			managerFindprojectInfoDTO=new ManagerFindprojectInfoDTO();
			managerFindprojectInfoDTO.setUserInfo(userInfo);
			managerFindprojectInfoDTO.setManagerFindprojectInfo(managerFindprojectInfo);
		}
		return managerFindprojectInfoDTO;
	}

/*建筑公司-收藏项目承包人yuxin3.11*/
	@Override
	public int companyCollectionManager(Map<String, Object> params) {
		int num=0;
		int isInsert=countService.isInsert(params);
		if(0==isInsert){
			num=companyFindManagerMapper.companyCollectionManager(params);
			Update update=new Update();
			int favoriteCount=countService.favoriteCount(params);
			update.set("favoriteCount",favoriteCount);
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("publishId"))),update,ManagerFindprojectInfo.class);
		}
		return num;
	}
/*建筑公司-取消收藏项目承包人yuxin3.11*/
	@Override
	public int cancelCollectionManager(Map<String, Object> params) {
		int num=0;
		int isInsert=countService.isInsert(params);
		if(isInsert>0){
			num=companyFindManagerMapper.cancelCollectionManager(params);
			Update update=new Update();
			int favoriteCount=countService.favoriteCount(params);
			update.set("favoriteCount",favoriteCount);
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("publishId"))),update,ManagerFindprojectInfo.class);
		}
		return num;
	}

/*建筑公司-收藏项目承包人列表yuxin3.11*/
	@Override
	public Page<CompanyFindManagerInfo> collectManagerList(Map<String, Object> params) {
		PageHelper.startPage((Integer) params.get("pageNum"),(Integer) params.get("pageSize"));
		Page<CompanyFindManagerInfo> page=companyFindManagerMapper.collectManagerList(params);
		return page;
	}
}
